package org.epidroid.epidroid.chronos.ui;

import java.util.List;

import org.epidroid.epidroid.R;
import org.epidroid.epidroid.chronos.engine.objects.ChronosNode;
import org.epidroid.epidroid.chronos.service.IChronosService;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

/*
 * TODO: create hierarchy to factor code between this class and ChronosGroupsAdapter
 */
public class ChronosFavAdapter extends ArrayAdapter<ChronosNode> {

	private List<ChronosNode> _items = null;
	private ChronosFavActivity _context = null;
	private IChronosService _service = null;

	public ChronosFavAdapter(ChronosFavActivity context,
			int textViewResourceId, List<ChronosNode> items,
			IChronosService service) {
		super(context, textViewResourceId);

		_context = context;
		_items = items;
		_service = service;
	}

	@Override
	public int getCount() {
		return _items.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ChronosNode t = this._items.get(position);
		View v = null;

		if (null != convertView)
			v = convertView;
		else {
			LayoutInflater inflater = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.chronos_group_item, null);
		}

		return handleViewItem(t, v, position);
	}

	private void setBackground(int position, View v) {
		if ((position % 2) != (getCount() % 2)) {
			v.setBackgroundResource(R.drawable.chronos_odd_column_bg);
		} else {
			v.setBackgroundResource(0);
		}
	}

	private View handleViewItem(final ChronosNode t, View v, int position) {
		TextView content = (TextView) v
				.findViewById(R.id.chronos_group_item_title);
		content.setText(t.getName());
		setBackground(position, v);

		final CheckBox checkbox = (CheckBox) v
				.findViewById(R.id.chronos_group_item_fav_button);
		checkbox.setChecked(_service.isFav(t));

		checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked)
					_service.setFavorite(t);
				else
					_service.unsetFavorite(t);
			}
		});

		content.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				checkbox.toggle();
			}
		});

		Button buttonDay = (Button) v
				.findViewById(R.id.chronos_group_item_button_day);
		Button buttonWeek = (Button) v
				.findViewById(R.id.chronos_group_item_button_week);

		buttonDay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				_context.openGroup(t, ChronosViewActivity.DISPLAY_DAY);
			}
		});
		buttonWeek.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				_context.openGroup(t, ChronosViewActivity.DISPLAY_WEEK);
			}
		});

		return (v);
	}

}
